package sanglh.edu.hsphysics.formulas;

import java.util.ArrayList;

import sanglh.edu.hsphysics.CONFIG;
import sanglh.edu.hsphysics.R;
import sanglh.edu.hsphysics.base.BazeActionBarActivity;
import sanglh.edu.hsphysics.utils.CommonUtils;
import sanglh.edu.hsphysics.utils.FragmentUtil;
import sanglh.edu.hsphysics.view.DepthPageTransformer;
import sanglh.edu.hsphysics.view.HackyViewPager;
import uk.co.senab.photoview.PhotoView;
import android.R.anim;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

public class FormulaActivity extends BazeActionBarActivity implements
		OnPageChangeListener {
	private static final String EXTRA_FORMULA_RES_COUNT = "EXTRA_FORMULA_RES_COUNT";
	private static final String EXTRA_FORMULA_RES_NAME = "EXTRA_FORMULA_RES_NAME";
	private static final String EXTRA_FORMULA_RES_ID = "EXTRA_FORMULA_RES_ID";
	private ViewPager mViewPager;
	private PagerAdapter mPagerAdapter;
	private ArrayList<FormularFragment> mFragments;
	private TextView mTextView;

	public static void start(Activity context, String res, int count) {
		Intent intent = new Intent(context, FormulaActivity.class);
		intent.putExtra(EXTRA_FORMULA_RES_NAME, res);
		intent.putExtra(EXTRA_FORMULA_RES_COUNT, count);
		context.startActivity(intent);
		context.overridePendingTransition(anim.fade_in, anim.fade_out);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_formula);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		mTextView = (TextView) findViewById(R.id.tv);
		mTextView.setText(String.format(getString(R.string.page), 1));
		mViewPager = (HackyViewPager) findViewById(R.id.pg);
		mPagerAdapter = new FormularPagerAdapter(getSupportFragmentManager());
		mFragments = new ArrayList<FormulaActivity.FormularFragment>();
		int count = getIntent().getIntExtra(EXTRA_FORMULA_RES_COUNT, 0);
		String resName = getIntent().getStringExtra(EXTRA_FORMULA_RES_NAME);
		for (int i = 0; i < count; i++) {
			mFragments
					.add(FormularFragment.newIntance(CommonUtils.getResource(
							getApplicationContext(),
							String.format("%s%d", resName, i))));
		}
		mViewPager.setPageTransformer(true, new DepthPageTransformer());
		mViewPager.setAdapter(mPagerAdapter);
		mViewPager.setOnPageChangeListener(this);
		showGuide();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.formula, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A simple pager adapter that represents 5 ScreenSlidePageFragment objects,
	 * in sequence.
	 */
	private class FormularPagerAdapter extends FragmentStatePagerAdapter {
		public FormularPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			return mFragments.get(position);
		}

		@Override
		public int getCount() {
			return mFragments.size();
		}
	}

	private static class FormularFragment extends Fragment {
		private int resId;

		public static FormularFragment newIntance(int resId) {
			FormularFragment fragment = new FormularFragment();
			Bundle bundle = new Bundle();
			bundle.putInt(EXTRA_FORMULA_RES_ID, resId);
			fragment.setArguments(bundle);
			return fragment;
		}

		@Override
		public void onCreate(Bundle savedInstanceState) {
			resId = getArguments().getInt(EXTRA_FORMULA_RES_ID);
			super.onCreate(savedInstanceState);
		}

		@Override
		public View onCreateView(LayoutInflater inflater,
				@Nullable ViewGroup container,
				@Nullable Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_formula,
					container, false);
			PhotoView img = (PhotoView) rootView.findViewById(R.id.img);
			img.setImageResource(resId);
			return rootView;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int arg0) {
		mTextView.setText(String.format(getString(R.string.page), arg0 + 1));
	}

	private void showGuide() {
		if (!isFinishing()) {
			mViewPager.post(new Runnable() {
				
				@Override
				public void run() {
					if (!CONFIG.isShowGuide(getApplicationContext())) {
						FragmentUtil.getDialog(FormulaActivity.this,
										getString(R.string.guide),getString(R.string.guide_msg), null,
								null, null).show();
						CONFIG.setShowGuide(getApplicationContext());
					}
					
				}
			});
		}

	}

}
